{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "702dee18",
   "metadata": {},
   "source": [
    "--------\n",
    "* 记录人：纪冰儿\n",
    "* 记录时间：2021-11-22\n",
    "* week13\n",
    "* 主要参考文档；\n",
    "> 1.[python官方文档](http://python.org/doc)\n",
    "> 2.[博客_函数]\n",
    "\n",
    "--------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28ea2e8f",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1.函数的定义：def\n",
    "> 2.函数名的定义规则-->变量名一样的规则\n",
    "> 3.调用函数的方式\n",
    "> 4.参数：\n",
    ">> 1.形式参数（定义函数）\n",
    ">> 2.实际参数（调用函数）\n",
    ">> 3.有多个参数时：\n",
    ">>> 1.位置参数（调用参数时）---> 关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>> 2.默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）\n",
    "# 本周知识\n",
    "> 1.函数的返回值\n",
    "> 2.函数的参数类型\n",
    "> 3.模块与函数的关系\n",
    "> 4.定义一个模块，然后调用模块\n",
    ">> A.调用模块\n",
    ">> B.调用模块中的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d3d0d66",
   "metadata": {},
   "source": [
    "## 1.函数的返回值\n",
    "\n",
    "> 1.返回值的关键字：return\n",
    "> 2.在其他的编程语言中，一般return一定要写，不写会报错；但是在python中，return可以不写，return默认为None\n",
    ">> 1.编程语言中的函数一定有返回值\n",
    ">> 2.返回的结果是一个值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b8b18536",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    # print(full_name)\n",
    "    #函数的返回结果为full_name\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aba9c171",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "li hua\n"
     ]
    }
   ],
   "source": [
    "get_formatted_name(\"li\",\"hua\")\n",
    "# print(get_formatted_name(\"li\",\"hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "77a56871",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "li hua\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "musician = get_formatted_name('li','hua')\n",
    "print(musician)# 输出的结果none表示执行了但不返回结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8cb57cc",
   "metadata": {},
   "source": [
    "## 2.函数的参数\n",
    "### 1.函数输入的参数\n",
    "> 1.函数是可以输入复杂的参数的\n",
    ">> 1.函数参数为列表\n",
    ">> 2.函数参数为字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ffa8cf61",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"函数输入一个列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b4e8bd25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lihua', 'ziqi', 'xiaofeng']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"lihua\",\"ziqi\",\"xiaofeng\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "12a2f4f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"函数输入一个字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c51d16d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#http://httpbin.org/get\n",
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Referer\": \"https://cn.bing.com/\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61ad73fd-133ed1853061112477de1aa4\"\n",
    "  }, \n",
    "  \"origin\": \"223.104.63.116\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "324f1d18",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Host': 'httpbin.org', 'Referer': 'https://cn.bing.com/', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43', 'X-Amzn-Trace-Id': 'Root=1-61ad73fd-133ed1853061112477de1aa4'}\n"
     ]
    }
   ],
   "source": [
    "# dit\n",
    "dict_test(httpbin_get[\"headers\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e36f0e63",
   "metadata": {},
   "source": [
    "> 2.传递任意数量的实参:\n",
    ">> 1.* args \n",
    ">>> 1.args 加星号可以接收多个数据\n",
    ">>> 2.args 可以将多个数据存在一个**元组**中，实际还是一个变量\n",
    ">> 2.** kwargs \n",
    ">>> 1.kwargs 可以接收多个数据（键值对） ==> 关键字参数\n",
    ">>> 2.kwargs 将多个 键值对 的数据存放在一个 **字典** 中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "66bafd7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "## * args 实验 将单个数据存放在元组中\n",
    "def make_food(food,*toppings):\n",
    "    \"\"\"打印顾客点的所有与配料\"\"\"\n",
    "    return food,toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "055b2152",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('mushroom', 'green peppers', 'extra cheese')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 回顾元组的定义\n",
    "toppings = \"mushroom\",\"green peppers\",\"extra cheese\"\n",
    "toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "19cc2768",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_food = make_food(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7fe4291c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "build_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d1ee2561",
   "metadata": {},
   "outputs": [],
   "source": [
    "## **kwargs 实践 将数据存放于字典中\n",
    "def person(name,**info):\n",
    "    \"\"\"打印个人的信息\"\"\"\n",
    "    return name,info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "9044476b",
   "metadata": {},
   "outputs": [],
   "source": [
    "build_person = person(\"lihua\",age=18,job=\"student\",address=\"南方学院\",email=\"970443057@qq.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4582f93d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('lihua', {'age': 18, 'job': 'student', 'address': '南方学院', 'email': '970443057@qq.com'})\n"
     ]
    }
   ],
   "source": [
    "print(build_person)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17920250",
   "metadata": {},
   "source": [
    "### 2.函数返回的结果\n",
    "\n",
    "> 1.函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构\n",
    ">> 1.函数返回一个集合\n",
    ">> 2.函数返回一个字典\n",
    "> 2.传递任意数量的实参:\n",
    ">> 1.* args\n",
    ">> 2.* kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0d9dfd93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个集合\n",
    "def search4vowels(word):\n",
    "    \"\"\"Return a boolean based on any vowels found\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels.intersection(set(word))\n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "786995e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'o', 'a', 'i'}\n"
     ]
    }
   ],
   "source": [
    "word = search4vowels('hello zhichao')\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "58eb0cc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回值为一个字典：\n",
    "def build_person(name,age,job):\n",
    "    \"\"\"返回一个字典，其中包含有关一个人的信息\"\"\"\n",
    "    person = {'name':name,'age':age,'job':job}\n",
    "    return person\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "16524d99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'lihua', 'age': 18, 'job': 'student'}\n"
     ]
    }
   ],
   "source": [
    "person = build_person('lihua',18,'student')\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3975c79a",
   "metadata": {},
   "source": [
    "# 3.模块\n",
    "> 1.模块一定是一个可执行的.py文件\n",
    "> 用户不需要去看模块内的代码\n",
    "> 2.调用模块的方法\n",
    ">> 1.import module_name 直接调用整个模块。此时模块的所有函数处于备用状态，使用的方法为：\n",
    "module_name.func_name()\n",
    ">>> 2. from module_name import func_time,此时，仅调用模块的某一个函数，只能使用这个模块的该函数，使用方法：func_time()\n",
    "* 方式一较全面 \n",
    "*方式二较高效"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f058ce6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i', 'o'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import study\n",
    "study.search4vowels('hello binger')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc91dc10",
   "metadata": {},
   "outputs": [],
   "source": [
    "from study import news_链接 # 该方法不需要再写模块的名字+func_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "318c12bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践： 抓取某个校园新闻的所有文本链接 --- 广州南方学院为例\n",
    "\n",
    "url = \"https://www.nfu.edu.cn/\"\n",
    "def news_链接(url,新闻分类,page):\n",
    "    \"\"\"抓取学校新闻的所有链接\"\"\"\n",
    "    news_链接 = []\n",
    "    for i in range(0,(page)):\n",
    "        if i == 1:\n",
    "            index_链接 = url+新闻分类+'index.html'\n",
    "            news_链接.append(index_链接)\n",
    "        else:\n",
    "            index_链接 = url + 新闻分类 + '/index'+ page+'.html'\n",
    "            news_链接.append(index_链接)\n",
    "    return news_链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b0f07f07",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "news_链接() missing 2 required positional arguments: '新闻分类' and 'page'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-53-5ae68cafc043>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 使用者只需要提供：url链接，学校分类的简称，\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnews_链接\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: news_链接() missing 2 required positional arguments: '新闻分类' and 'page'"
     ]
    }
   ],
   "source": [
    "# 使用者只需要提供：url链接，学校分类的简称，\n",
    "result = news_链接('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a6b86fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
